<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ADM_Controller
 *
 * @author Lucas Pinheiro Pinto Louco
 */
class ADM_Controller extends CMS_Controller {

//put your code here
    protected $usuario = null;

    public function __construct() {
        parent::__construct();
        //$this->parser->add_js('functions');
        $this->parser->add_js('jquery-ui.min');

        $this->parser->add_js('config_' . $this->config->item('language'), TRUE);
        $this->parser->add_js('config_jquery_ui', TRUE);
        $this->parser->add_js('config_datables', TRUE);

        $this->parser->add_js('lang_' . $this->config->item('language'));

        $this->parser->add_js('functions');
        $this->parser->add_js('plugins');
        $this->parser->add_js('editor');
        $this->parser->add_js('calendar');
        $this->parser->add_js('flot');
        $this->parser->add_js('elfinder');

        $this->parser->add_js('datatables');

        $this->parser->add_js('wl_Alert');
        $this->parser->add_js('wl_Autocomplete');
        $this->parser->add_js('wl_Breadcrumb');
        $this->parser->add_js('wl_Calendar');
        $this->parser->add_js('wl_Chart');
        $this->parser->add_js('wl_Color');
        $this->parser->add_js('wl_Date');

        $this->parser->add_js('wl_Editor');
        $this->parser->add_js('wl_File');
        $this->parser->add_js('wl_Dialog');
        $this->parser->add_js('wl_Fileexplorer');
        $this->parser->add_js('wl_Form');
        $this->parser->add_js('wl_Gallery');
        $this->parser->add_js('wl_Multiselect');
        $this->parser->add_js('wl_Number');
        $this->parser->add_js('wl_Password');
        $this->parser->add_js('wl_Slider');
        $this->parser->add_js('wl_Store');
        $this->parser->add_js('wl_Time');
        $this->parser->add_js('wl_Valid');
        $this->parser->add_js('wl_Widget');


        $this->parser->add_js('config_widgets');
        $this->parser->add_js('config_form');

        $this->parser->add_js('jquery.maskedinput-1.3');

        $this->load->add_module('login');
        $this->load->library('permissions');
        $this->load->library('login/tank_auth');
        if ($this->tank_auth->get_user_id() == FALSE) {
            $this->session->sess_destroy();
            redirect(base_cms() . $this->permissions->get_url('login_login_index'));
        } else {
            $this->usuario = new stdClass();
            $this->usuario->user_id = $this->tank_auth->get_user_id();
            $this->usuario->username = $this->tank_auth->get_username();
            $this->session->set_userdata('user_id', $this->usuario->user_id);
        }
    }

    public function checar_metodo($_metodo) {
        if ($_metodo != 'cms/rota') {
            $url = array();
            foreach ($this->uri->segment_array() as $chave => $valor) {
                if ($chave > 1 && $chave < 5) {
                    $url[] = $valor;
                }
            }
            $retorno = $this->permissions->_checked(implode('_', $url));

            $this->session->set_userdata('method_query_executado', implode('_', $url));
            $entrada = $this->input->is_ajax_request();


            if ($retorno == false && $entrada == false) {
                redirect(base_cms() . 'login/negada/index');
            }if ($retorno == false && $entrada == true) {
                echo json_encode($this->error_metodo('ajax', ''));
                exit;
            }
        }
    }

    public function error_metodo($_retorno = 'html', $_id = '') {
        $this->lang->load('errors');
        $v_dados = array();
        $v_dados['heading'] = $this->lang->line('erros_messagem_heading');
        $v_dados['message'] = $this->lang->line('erros_messagem_message');
        $v_dados['messagem_link'] = $this->lang->line('erros_messagem_link');
        if ($_retorno == 'html') {
            $this->parser->parse_cms('erros_error_404', $v_dados);
        } else {
            $dados = array();
            $dados['id'] = $_id;
            $dados['cod'] = 111;
            $dados['msg'] = $this->lang->line('erros_short_messagem_message');
            $dados['header'] = $this->lang->line('erros_short_messagem_heading');
            return $dados;
        }
    }

    function _show_message($message, $heading = "Login") {
        $this->parser->parse_cms('erros_general_message', array('heading' => $heading, 'message' => $message));
    }

}
